package com.wiken.storeservice.controller;


import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.R;
import com.wiken.storeservice.entity.Store;
import com.wiken.storeservice.entity.StoreType;
import com.wiken.storeservice.entity.Type;
import com.wiken.storeservice.service.impl.StoreTypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/storeservice/storetype")
public class StoreTypeController {

    @Autowired
    StoreTypeServiceImpl storeTypeService;

    @PostMapping("addStoreType")
    public R addStoreType(@RequestBody StoreType storeType, @RequestHeader("Authorization") String token){
        boolean success = storeTypeService.addStoreType(storeType, token);

        if(!success) return R.error().code(Code.ERROR).message("添加店铺类型失败");

        return R.ok().code(Code.SUCCESS).message("添加店铺类型成功");
    }

    @PutMapping("updateStoreType")
    public R updateStoreType(@RequestBody StoreType storeType, @RequestHeader("Authorization") String token){
        boolean success = storeTypeService.updateStoreType(storeType, token);

        if(!success) return R.error().code(Code.ERROR).message("更新店铺类型失败");

        return R.ok().code(Code.SUCCESS).message("更新店铺类型成功");
    }

    @PutMapping("updateByStoreId")
    public R updateByStoreId(@RequestBody StoreType storeType, @RequestHeader("Authorization") String token){
        boolean success = storeTypeService.updateByStoreId(storeType, token);

        if(!success) return R.error().code(Code.ERROR).message("更新店铺类型失败");

        return R.ok().code(Code.SUCCESS).message("更新店铺类型成功");
    }
    @DeleteMapping("delStoreType")
    public R delStoreType(@RequestBody StoreType storeType, @RequestHeader("Authorization") String token){
        boolean success = storeTypeService.delStoreType(storeType, token);

        if(!success) return R.error().code(Code.ERROR).message("删除店铺类型失败");

        return R.ok().code(Code.SUCCESS).message("删除店铺类型成功");
    }

    @DeleteMapping("delStoreTypeByStoreId")
    public R delStoreTypeByStoreId(@RequestBody StoreType storeType, @RequestHeader("Authorization") String token){
        boolean success = storeTypeService.delStoreTypeByStoreId(storeType, token);

        if(!success) return R.error().code(Code.ERROR).message("删除店铺类型失败");

        return R.ok().code(Code.SUCCESS).message("删除店铺类型成功");
    }

    @PostMapping("getStoreTypes")
    public R getStoreTypes(@RequestBody Store store){
        List<StoreType> storeTypeList = storeTypeService.getStoreTypeList(store);

        return R.ok().code(Code.SUCCESS).message("获取店铺类型成功").data("storeTypeList", storeTypeList);
    }

    @PostMapping("getTypeByStoreId")
    public R getTypeByStoreId(@RequestBody Store store){
        Type type = storeTypeService.getTypeByStoreId(store);

        return R.ok().code(Code.SUCCESS).message("获取类型成功").data("type", type);
    }
}
